package com.lzy.pattern.observer.userdefined;

import java.util.ArrayList;
import java.util.List;

/**
 * @ClassName: SubscriptionSubject
 * @Description: ConcreteSubject：具体主题（具体被观察者），该角色将有关状态存入具体观察者对象，在具体主题的内部状态发生改变时，给所有注册过的观察者发送通知。
 * @Author: LiuZy
 * @Date: 2022/5/1 15:49
 * @Version: 1.0
 */
public class SubscriptionSubject implements Subject{

    private List<Observer> observerList = new ArrayList<>();

    @Override
    public void attach(Observer observer) {
        observerList.add(observer);
    }

    @Override
    public void detach(Observer observer) {
        observerList.remove(observer);
    }

    @Override
    public void notify(String msg) {
        for (Observer observer : observerList) {
            observer.update(msg);
        }
    }
}